#!/usr/bin/env python
import socket
import time

class Server(object):
	ctx = None
	def __init__(self, ip, port):
		assert isinstance(port,int),"'port' must be a integer."
		assert isinstance(ip,str),"'ip' must be a string."

		self.client_conn_ = None
		self.host_ = ip
		self.port_ = port
		self.sock_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		print("Server bind to {0}:{1}.".format(self.host_, self.port_))
		self.sock_.bind((self.host_, self.port_))
		self.sock_.listen(5)

	def accept(self):
		self.client_conn_, self.client_addr_ = self.sock_.accept()

	def send(self,msg):
		#print("Send > {0}".format(msg))
		#print(time.time())
		msg = msg + "\r\n"
		ret = self.client_conn_.send(msg)
		#print(time.time())

	def recv(self):
		return self.client_conn_.recv(1024)

	def close_client(self):
		self.client_conn_.close()
		self.client_conn_ = None
